<?php
if(isset($_REQUEST['aksi_daftar_pegawai'])){
	include '../class/class.usulan_peg.php';
	include '../class/class.pegawai.php';
	include '../class/class.refcuti.php';
	include '../class/class.tujuan.php';
	include '../class/class.golongan.php';
	include '../class/class.jabatan.php';
	include '../class/class.level.php';

	session_start();

	$nip=$_SESSION['nip'];
	$pegawaiDao= new PegawaiDao();
	$usulanPegDao=new UsulanPegDao();
	$refCutiDao=new RefCutiDao();
	$tujuanDao=new TujuanDao();

	$peg=$pegawaiDao->getPegawai($nip);

	$unit=$peg['unit'];

	$allPeg=$pegawaiDao->getAllPegawai($unit);

	$usulan=array();
	$arr=array();
	$x=0;
	for($i=0;$i<count($allPeg);$i++){
		$usulan[$i]=$usulanPegDao->getAllToProses($allPeg[$i]['nip']);
		if($usulan[$i]!=false){
			for($j=0;$j<count($usulan[$i]);$j++){
				$arr[$x]=$usulan[$i][$j];
				$x++;
			}
		}
	}

    $msg = false;
    $daftar=array();

    if(count($arr)>0){
    	$msg=true;
    	for($i=0;$i<count($arr);$i++){
    		$daftar[$i]=array(
    			'usulan' => $arr[$i],
    			'pegawai' => $pegawaiDao->getPegawai($arr[$i]['nip']),
    			'ref' => $refCutiDao->getCuti($arr[$i]['id_cuti']),
    			'tujuan' => $tujuanDao->getTujuan($arr[$i]['id_tujuan']),
    			);
    	}
    }

    $data = array(
        'msg' => $msg,
        'daftar' => $daftar,
    );

    echo json_encode($data);
}


if(isset($_REQUEST['aksi_daftar_pegawai_sudah'])){
	include '../class/class.usulan_peg.php';
	include '../class/class.pegawai.php';
	include '../class/class.refcuti.php';
	include '../class/class.tujuan.php';
	include '../class/class.golongan.php';
	include '../class/class.jabatan.php';
	include '../class/class.level.php';

	session_start();

	$nip=$_SESSION['nip'];
	$pegawaiDao= new PegawaiDao();
	$usulanPegDao=new UsulanPegDao();
	$refCutiDao=new RefCutiDao();
	$tujuanDao=new TujuanDao();

	$peg=$pegawaiDao->getPegawai($nip);

	$unit=$peg['unit'];

	$allPeg=$pegawaiDao->getAllPegawai($unit);

	$usulan=array();
	$arr=array();
	$x=0;
	for($i=0;$i<count($allPeg);$i++){
		$usulan[$i]=$usulanPegDao->getAllSudahProses($allPeg[$i]['nip']);
		if($usulan[$i]!=false){
			for($j=0;$j<count($usulan[$i]);$j++){
				$arr[$x]=$usulan[$i][$j];
				$x++;
			}
		}
	}

    $msg = false;
    $daftar=array();

    if(count($arr)>0){
    	$msg=true;
    	for($i=0;$i<count($arr);$i++){
    		$daftar[$i]=array(
    			'usulan' => $arr[$i],
    			'pegawai' => $pegawaiDao->getPegawai($arr[$i]['nip']),
    			'ref' => $refCutiDao->getCuti($arr[$i]['id_cuti']),
    			'tujuan' => $tujuanDao->getTujuan($arr[$i]['id_tujuan']),
    			);
    	}
    }

    $data = array(
        'msg' => $msg,
        'daftar' => $daftar,
    );

    echo json_encode($data);
}

if(isset($_REQUEST['aksi_list_usulan'])){
	include '../class/class.usulan_kppn.php';
	include '../class/class.pegawai.php';

	session_start();
	$nip=$_SESSION['nip'];

	$pegawaiDao=new PegawaiDao();
	$usulanKppnDao= new UsulanKppnDao();
	$pegawai=$pegawaiDao->getPegawai($nip);

	$unit=$pegawai['unit'];
	$msg=false;
	$usulan=$usulanKppnDao->getAllByUnit($unit);

	if($usulan!=false){
		$msg=true;
	}

	$data=array(
		'msg' => $msg,
		'usulan' => $usulan,
		);

	echo json_encode($data);
}

if(isset($_REQUEST['aksi_proses_usulan'])){
	include '../class/class.usulan_kppn.php';
	include '../class/class.usulan_peg.php';
	include '../class/class.detail_usulan_kppn.php';
	include '../class/class.pegawai.php';
	$ids=$_REQUEST['ids'];

	session_start();
	$nip=$_SESSION['nip'];

	$pegawaiDao=new PegawaiDao();
	$usulanKppnDao= new UsulanKppnDao();
	$pegawai=$pegawaiDao->getPegawai($nip);

	$unit=$pegawai['unit'];

	// $id=explode(",",$ids);

	$usulanKppn=new UsulanKppn();
	$detailDao= new DetailKppnDao();
	$usulanPegDao= new UsulanPegDao();
	$detail=array();

	$usulanKppn->nomorSurat="Kosong";
	$usulanKppn->tglSurat=date('Y-m-d');
	$usulanKppn->unit=$unit;

	$msg=false;

	$usulanKppnDao=  new UsulanKppnDao();

	$insert=$usulanKppnDao->createUsulan($usulanKppn);

	if($insert!=false){
		$msg=true;
		for($i=0;$i<count($ids);$i++){
			$detail[$i]=new DetailKppn();

			$detail[$i]->id_usulan_kppn=$insert;
			$detail[$i]->id_usulan_peg=$ids[$i];

			$cek=$detailDao->createDetail($detail[$i]);
			if($cek){
				$usulanPegDao->updateUsulanProses($ids[$i]);
			}
		}
	}

	$data=array(
		'msg' => $msg,
		);

	echo json_encode($data);

}

?>